/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.math.BigDecimal;
import java.util.*;
import java.sql.Timestamp;
import org.ofbiz.entity.*;
import org.ofbiz.entity.condition.*;
import org.ofbiz.entity.util.*;
import org.ofbiz.base.util.*;
import org.ofbiz.base.util.collections.*;
import org.ofbiz.order.order.*;
import org.ofbiz.party.contact.*;
import org.ofbiz.product.inventory.InventoryWorker;
import org.ofbiz.product.catalog.CatalogWorker;
import org.ofbiz.accounting.payment.*;

import javolution.util.FastMap;

orderId = currentOrderId;
orderHeader = null;
orderItems = null;
orderAdjustments = null;
approvedOrderItems = null;

if (orderId) {
    orderHeader = delegator.findOne("OrderHeader", [orderId : orderId], false);
}

if (orderHeader) {
    // note these are overridden in the OrderViewWebSecure.groovy script if run
    context.hasPermission = true;
    context.canViewInternalDetails = true;

    orderReadHelper = new OrderReadHelper(orderHeader);
    orderItemList = orderReadHelper.getOrderItems();
    orderItems = EntityUtil.filterByCondition(orderItemList, EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "ITEM_CREATED"));
    approvedOrderItems = EntityUtil.filterByCondition(orderItemList, EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "ITEM_APPROVED"));
    orderAdjustments = orderReadHelper.getAdjustments();
    orderHeaderAdjustments = orderReadHelper.getOrderHeaderAdjustments();
    orderSubTotal = orderReadHelper.getOrderItemsSubTotal();
    orderTerms = orderHeader.getRelated("OrderTerm", null, null, false);
    context.orderHeader = orderHeader;
    context.orderReadHelper = orderReadHelper;
    context.orderItems = orderItems;
	context.approvedOrderItems = approvedOrderItems;
    context.orderItemSize = orderItems.size();
    context.orderAdjustments = orderAdjustments;
    context.orderHeaderAdjustments = orderHeaderAdjustments;
    context.orderSubTotal = orderSubTotal;
    context.currencyUomId = orderReadHelper.getCurrency();
    context.orderTerms = orderTerms;
}
/*User Login*/
userPerson = [:];
firstName = null;
lastName = null;
nickName = null;

partyNameView = delegator.findOne("PartyNameView", [partyId: userLogin.partyId], true);
firstName = partyNameView.firstName;
lastName = partyNameView.lastName;
nickName = partyNameView.nickName;
context.currentUserName = (firstName != null ? firstName : "") + (firstName != null ? " " : "") + (lastName != null ? lastName : "") + (lastName != null ? " " : "") + (nickName != null ? "(" + nickName + ")": "");

// Logo
logoContentId = null;
webSiteContentList = delegator.findByAnd("WebSiteContent", [webSiteId: context.webSiteId, webSiteContentTypeId: "WEBSITE_LOGO"], null, false);
webSiteContentList = EntityUtil.filterByDate(webSiteContentList);
if (webSiteContentList) {
    logoContentId = webSiteContentList[0].contentId;
}
context.logoContentId = logoContentId;